package com.czz.ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czz.ucenter.entity.Audit;
import com.czz.ucenter.mapper.AuditMapper;
import com.czz.ucenter.service.AuditService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @ClassName: AuditServiceImpl
 * @Author Czz
 * @Date 2020/2/21
 * @Time 16:00
 **/
@Service
public class AuditServiceImpl extends ServiceImpl<AuditMapper, Audit> implements AuditService {

    @Autowired
    AuditMapper auditMapper;


    @Override
    public Audit getAuditByUserId(String userId) {
        QueryWrapper<Audit> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        Audit audit = auditMapper.selectOne(queryWrapper);
        return  audit;
    }

    @Override
    @Transactional
    public void updateAudit(Audit audit) {
        //判断申请状态
        Audit temp = auditMapper.selectById(audit.getAuditId());

        //如果未审核则直接修改
        if (temp.getStatus().equals(0)){
            auditMapper.updateById(audit);
        }

        //如果审核失败则重新提交申请
        if (temp.getStatus().equals(2)){
            //删除原有申请
            int result = auditMapper.deleteById(audit.getAuditId());
            //新增申请
            if (result > 0){
                audit.setAuditId("");
                auditMapper.insert(audit);
            }
        }
    }
}
